var $ = layui.$;
var layer = layui.layer;
var form = layui.form;
var table = layui.table;
var element = layui.element;
var tree = layui.tree;
var util = layui.util;
var jQuery = $;
var treeTable = layui.treeTable;
var dropdown = layui.dropdown;
var colorpicker = layui.colorpicker;
var laydate = layui.laydate;
var statusArr = [{id:1,name:'未开始'},{id:2,name:'进行中'},{id:3,name:'已暂停'},{id:5,name:'已提测'},{id:6,name:'已上线'},{id:7,name:'已外包'},{id:8,name:'外包已完成'},{id:100,name:'待删除'}];

var c = {
	loading:function(){
		c.lastLoadIndex = layer.load(0);
	},
	loaded:function(){
		layer.close(c.lastLoadIndex);
	},
	message:function(message,lr){
		var l = lr ? lr : layer;
		l.msg('<span style="color:#FFFFFF;">'+message+'</span>');
	},
	opSpace:function(){
		return '&nbsp;&nbsp;&nbsp;&nbsp;';
	},
	putPrefix:function(data,prefix){
		var o = {};
		for( var key in data ){
			o[prefix+key] = data[key];
		}
		return o;
	},
	getRequestParams:function() {
		var o = {};
		var href = location.href;
		if(href.indexOf('?')==-1){
			return o;
		}
		href = href.substr(href.indexOf('?')+1);
		href = href.split('&');
		$.each(href,function (i,row) {
			var arr = row.split('=');
			o[arr[0]] = decodeURIComponent(arr[1]);
		});
		return o;
	},
	closePopWindow:function(name){
		layer.close(layer.getFrameIndex(c.getPopFrame(name).name));
		parent.layer.close(parent.layer.getFrameIndex(c.getPopFrame(name).name));
	},
	getPopFrame:function(name){
		name = name ? name : 'first';
		var ifrs = c.findAllFrame(top);
		for( var i=0 ; i<ifrs.length ; i++ ){
			if(ifrs[i].iframe==undefined)continue;
			if( ifrs[i].iframe == name ){
				return ifrs[i];
			}
		}
	},
	findAllFrame:function(frame){
		var resultFrames = [];
		var ifrs = frame.frames;
		if( ifrs.length == 0 ){
			return [];
		}
		for( var i=0 ; i<ifrs.length ; i++ ){
			resultFrames.push(ifrs[i]);
			if( ifrs[i].frames.length > 0 ){
				var result = c.findAllFrame(ifrs[i]);
				if( result.length > 0 ){
					resultFrames = resultFrames.concat(result);
				}
			}
		}
		return resultFrames;
	},
    request:function(url,data,callback){
		c.post({
			url:url,
			data:data,
			success:callback
		});
	},
    post:function(opts){
		var loginUser = c.getLoginUser();
		
		if(loginUser.userId){
			if( opts.data ){
				opts.data.createUserId = loginUser.userId;
				opts.data.consumerId = loginUser.consumerId;
			}else{
				opts.data = {createUserId:loginUser.userId,consumerId:loginUser.consumerId};
			}
		}
		
		var options = {
			//dataType:'json',
			type:'POST',
			beforeSend: function(request) {
				request.setRequestHeader("X-Admin-Token",loginUser.tk);
			}
		};
		$.extend(options,opts);
		options.url = '/tasks/'+opts.url;
		options.success = function(resp){
			var resp = $.parseJSON(resp);
			if(resp.error=='no permission' || resp.code == 666){
				top.layer.alert('登录超时或者无权限，需要重新登录', function(index){
					document.cookie = "data={};path=/";
				  	top.location.href='/login.html';
				  	top.layer.close(index);
				});       
				
			}else{
				opts.success(resp);
			}
		}
		options.error = function(resp){
			c.loaded();
			layer.alert('网络错误');
		}
		$.ajax(options);
	},
    table:function(opts){
		var loginUser = c.getLoginUser();
		var options = {
			method:'post',
			where:{createUserId:loginUser.userId,consumerId:loginUser.consumerId},
			cellExpandedMode:'tips',
			headers:{
				"X-Admin-Token":loginUser.tk
			},
			parseData: function(res){ // res 即为原始返回的数据
				return {
				"code": res.code, // 解析接口状态
				"msg": res.message, // 解析提示文本
				"count": res.data.total, // 解析数据长度
				"data": res.data.list // 解析数据列表
				};
			},
			page: true //开启分页
		};
		if( opts.where ){
			opts.where.createUserId = loginUser.userId;
			opts.where.consumerId = loginUser.consumerId;
		}
		opts.url = '/tasks'+opts.url;
		$.extend(options,opts);
		if( options.data ) options.url = null;
		return table.render(options);
	},
    getCtxWidth:function(){
		return top.$('#ifra001').width();
	},
	getCtxHeight:function(){
		return top.$('#ifra001').height();
	},
    window:function(opts){
		var options = {
            type: 2,
            title: false,
            anim: 'slideDown' // 从上往下
        };
		$.extend(options,opts);
		var area = options.area;
		options.area[0] = parseInt(area[0].replace('px','')) > c.getCtxWidth()-15 ? (c.getCtxWidth()-15)+'px' : area[0];
		options.area[1] = parseInt(area[1].replace('px','')) > c.getCtxHeight()-28 ? (c.getCtxHeight()-28)+'px' : area[1];

		layer.open(options);
	},
    uuid:function(){
		const timestamp = new Date().getTime().toString();
		const random = Math.random().toString().substring(2, 8);
		return timestamp + random;
	},
    getLoginUser:function(){
		var loginUser = null;
		try{
			loginUser = c.getLoginInfo();
			if( $.trim(loginUser.tk).length == 0 ){
				location.href = '/login.html';
			}
			if( loginUser == null || $.trim(loginUser.userId).length == 0 ){
				location.href = '/login.html';
			}
		}catch(e){
			location.href = '/login.html';
		}
		return loginUser;
	},
    getLoginInfo:function(){
		var value = document.cookie;
		value = value.split(';');
		for( var i=0 ; i<value.length ; i++ ){
			var m = value[i].split('=');
			if( $.trim(m[0]) == 'data' ){
				return $.parseJSON(m[1]);
			}
		}
	}

}